package com.caocaog.market.controller;

import com.caocaog.market.annotation.IgnoreAuth;
import com.caocaog.market.service.IOrderLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.Limit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * 订单统计数据api
 *
 * @author chechen
 * @date 2021-01-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "statisticsApi", tags = "API订单统计数据")
@RequestMapping("/api/statistics")
public class ApiStatisticsController extends BaseController {

    private final IOrderLineService orderLineService;

    @IgnoreAuth
    @Limit(key = "statistics", period = 60, count = 60, name = "获取销量", prefix = "limit")
    @ApiOperation(value = "获取销量", notes = "传入参数：type 0今日，1昨日，7七天，30三十天，不传值是总销量；返回对象：count总数，goodsName商品名称，specName规格名称，imageUrl图片地址，price单价")
    @GetMapping(value = "/getSales")
    public Result<?> getSales(@NotBlank(message = "店铺id不能为空") @RequestParam String shopId, @RequestParam(required = false) Integer type) {
        if (type == null || type == 0 || type == 1 || type == 7 || type == 30) {
            return Result.OK(orderLineService.getSales(shopId, type));
        }
        return Result.error("请传入正确的type值，type 0今日，1昨日，7七天，30三十天，不传值是总销量");
    }

    @IgnoreAuth
    @Limit(key = "statistics", period = 60, count = 60, name = "获取订单量", prefix = "limit")
    @ApiOperation(value = "获取订单量", notes = "返回对象：最外层对象分别是 all总订单，takeout配送订单，pickUp自提订单；里面的对象是 total总订单，today今天，yesterday昨天，thisWeek七天，thisMonth三十天")
    @GetMapping(value = "/getOrderAmount")
    public Result<?> getOrderVolume(@RequestParam String shopId) {
        return Result.OK(orderLineService.getOrderAmount(shopId));
    }

    @IgnoreAuth
    @Limit(key = "statistics", period = 60, count = 60, name = "获取营业额", prefix = "limit")
    @ApiOperation(value = "获取营业额", notes = "返回对象：total总营业额，today今天，yesterday昨天，thisWeek七天，thisMonth三十天")
    @GetMapping(value = "/getSumOfBusiness")
    public Result<?> getSumOfBusiness(@RequestParam String shopId) {
        return Result.OK(orderLineService.getSumOfBusiness(shopId));
    }
}
